package com.example.Service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;

import com.example.FileSearch.FileSearcher;
import com.example.FileSearch.SQLiteFileRecorder;

/**
 * Created by fany on 14-1-22.
 */
public class FileSearchService extends Service {

    @Override
    public void onCreate(){
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(_thread == null){
            _thread = new FileSearchThread(this, true);
            _thread.start();
        }
        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private FileSearchThread _thread;

    class  FileSearchThread extends Thread{
        public FileSearchThread(Context context, boolean forceReload){
            _forceReload = forceReload;
            _context = context;
        }

        @Override
        public void run() {
            super.run();
            SQLiteFileRecorder recorder = new SQLiteFileRecorder(_context);
            FileSearcher searcher = new FileSearcher(recorder);
            searcher.LoadFiles(Environment.getExternalStorageDirectory().getAbsolutePath(), _forceReload);
        }
        private boolean _forceReload;
        private Context _context;
    }
}
